/**
 * Copyright (C) 2013 SHANGHAI VOLKSWAGEN, All rights reserved.
 * License version 1.0, a copy of which has been included with this.
 *
 * @File name：com.bwie.system.service.impl.SystemServiceImpl
 * @Create on：2024/3/22
 * @Author：liuchang
 */
package com.bwie.patient.service.impl;

import cn.hutool.bloomfilter.BloomFilterUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bwie.common.constants.TokenConstants;
import com.bwie.common.domain.Dept;
import com.bwie.common.domain.Evaluate;
import com.bwie.common.domain.Receive;
import com.bwie.common.domain.User;
import com.bwie.common.domain.response.DeptResponse;
import com.bwie.common.domain.response.DescribeResponse;
import com.bwie.common.domain.response.MedicResponse;
import com.bwie.common.result.Result;
import com.bwie.common.utils.JwtUtils;
import com.bwie.patient.mapper.PatientMapper;
import com.bwie.patient.service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * <p>ClassName：SystemServiceImpl</p >
 * <p>Description：</p >
 * <p>Author：liuchang</p >
 * <p>Date：2024/3/22</p >
 * @author 任旭浩
 */
@Service
public class PatientServiceImpl implements PatientService {
    @Autowired
    private PatientMapper patientMapper;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Autowired
    private HttpServletRequest request;
    @Override
    public Result<List<Dept>> patientList() {
        String key = "sky";
        if(redisTemplate.hasKey(key)){
            List<String> objectList = redisTemplate.opsForList().range(key, 0, -1);
            ArrayList<Dept> newlist = new ArrayList<>();
            for (Object object : objectList) {
                String object1 = (String) object;
                Dept dtoApply = JSON.parseObject(object1, Dept.class);
                newlist.add(dtoApply);
            }
            return Result.success(newlist,"1");
        }
        List<Dept> depts=patientMapper.patientList();
        for (Dept dept : depts) {
            redisTemplate.opsForList().rightPush(key, JSON.toJSONString(dept));
        }
        return Result.success(depts,"2");
    }
    @Override
    public Result<List<DeptResponse>> patientsList(Integer id) {
        List<DeptResponse> depts=patientMapper.patientsList(id);
        return Result.success(depts);
    }
    @Override
    public Result<List<User>> medicList(MedicResponse medicResponse) {
        List<User> users=patientMapper.medicList(medicResponse);
        return Result.success(users);
    }
    @Override
    public Result lineupInsert(DescribeResponse describeResponse) {
        Date date = new Date();
        String token = request.getHeader(TokenConstants.TOKEN);
        String userKey = JwtUtils.getUserKey(token);
        String s = redisTemplate.opsForValue().get(TokenConstants.LOGIN_TOKEN_KEY + userKey);
        User users = JSONObject.parseObject(s, User.class);
        Receive receive = new Receive();
        receive.setReceiveDate(date);
        receive.setReceivePrice(describeResponse.getPrice());
        receive.setPatientsId(users.getUserId());
        receive.setDiseaseDescription(describeResponse.getDisease());
        receive.setDoctorId(describeResponse.getUser().getUserId());
        Integer i=patientMapper.lineupInsert(receive);
        return Result.success("排队成功");
    }
    @Override
    public Result<List<Receive>> lineupList() {
        List<Receive> receives=patientMapper.lineupList();
        return Result.success(receives);
    }
    @Override
    public Result<List<User>> doctorList() {
        List<User> users=patientMapper.doctorList();
        return Result.success(users);
    }
    @Override
    public Result evaluateInsert(Evaluate evaluate) {
        Date date = new Date();
        evaluate.setDatetime(date);
        Integer i=patientMapper.evaluateInsert(evaluate);
        return Result.success("评价完成");
    }
}
